IF ELIF and ELSE Statements


In [2]:
if True:
    print("true")


true

In [3]:
x = False

In [4]:
if x == True:
    print("True")
else:
    print("False")


False

In [10]:
x  = "isadacici"

if x == "Axis":
    print("This is a {} BANK".format(x))
elif x == "icici":
    print("This is an {} BANK".format(x))
elif x == "Hdfc":
    print("This is an {} BANK".format(x))
else:
    print("out of bound")


out of bound

FOR LOOPS


In [14]:
l  = [1,2,3,4,5]
l


Out[14]:
[1, 2, 3, 4, 5]

In [18]:
for item in l:
    print(item)


1
2
3
4
5

In [19]:
l = [1,2,3,4,5,6,7,8,9,10]

In [21]:
for n in l:
    if n%2 == 0:
        print("{} is an Even Number".format(n))
    else:
        print("{} is an odd Number".format(n))


1 is an odd Number
2 is an Even Number
3 is an odd Number
4 is an Even Number
5 is an odd Number
6 is an Even Number
7 is an odd Number
8 is an Even Number
9 is an odd Number
10 is an Even Number

In [25]:
l = [1,2,3,4,5]

In [26]:
listsum = 0

for i in l:
    listsum += i
        
print("The Total value of listSum is {}".format(listsum))


The Total value of listSum is 15

In [27]:
# tuple unpacking

In [28]:
t = (1,2,3)

In [29]:
for i in t:
    print(i)


1
2
3

In [30]:
l = [(1,2),(3,4),(5,6),(7,8)]

In [33]:
for t1,t2 in l:
    #print(t1)
    print(t2)


2
4
6
8

In [34]:
d = {"K1":1,"K2":2,"K3":3}

In [38]:
for k,v in d.iteritems():
    print(k,v)


('K3', 3)
('K2', 2)
('K1', 1)

In [39]:
for t1,t2 in l:
    print(t1+t2)


3
7
11
15

While Loops


In [45]:
x = 0

while x < 10:
    print("x is currently {}".format(x))
    x += 1


x is currently 0
x is currently 1
x is currently 2
x is currently 3
x is currently 4
x is currently 5
x is currently 6
x is currently 7
x is currently 8
x is currently 9
print(x)

In [49]:
x = 0
while x <10:
    print("x is {} and less than 10".format(x))
    print("Adding 1 to x")
    x +=1
    
    if x == 3:
        print("x is equal to 3")
        break
    else:
        print("continuing...")
        continue


x is 0 and less than 10
Adding 1 to x
continuing...
x is 1 and less than 10
Adding 1 to x
continuing...
x is 2 and less than 10
Adding 1 to x
x is equal to 3

In [ ]: